package com.wutianyi.favority.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wutianyi.favority.FavorityRepository;
import com.wutianyi.favority.bean.Folder;
import com.wutianyi.favority.bean.User;
import com.wutianyi.favority.service.FavorityServiceLocator;
import com.wutianyi.favority.utils.FavorityUtils;

public class LinkIndexServlet extends HttpServlet {

	/**
     * 
     */
	private static final long serialVersionUID = -2007910059301255893L;

	private static FavorityRepository repository = FavorityServiceLocator
			.getRepository();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		int folderId = FavorityUtils.stringToInt(req.getParameter("folderId"),
				-1);
		long uid = FavorityUtils.stringToLong(req.getParameter("uid"), -1);

		User user = repository.getUser(uid);
		if (null != user) {
			Folder folder = user.getFolderById(folderId);
			if (null != folder) {
				req.setAttribute("name", folder.getName());
				req.setAttribute("links", folder.getLinks());
			}
		}
		req.setAttribute("folderId", folderId);
		req.setAttribute("uid", uid);
		req.getRequestDispatcher("linkIndex.jsp").forward(req, resp);
	}

}
